#include "stdafx.h"
#include "ServiceImpLgc.h"
#include "Entity/EntityManager.h"
#include "Entity/EntityBuilder.h"
#include "Scene/SceneManagerSvr.h"
#include "Scene/SceneMsgHandler.h"
#include "Message/MsgCategory.h"
#include "Message/MsgHandlerDispatchByCate.h"
#include "Message/DBMessage.h"
#include "login/LoginMsgHandler.h"
#include "Player/PlayerMsgHandler.h"
#include "Manager/DataManagerLua.h"
#include "Manager/DataManagerImp.h"
#include "Lua/LuaWrapper.h"
#include "Data/DataLua.h"
#include "Manager/DataManagerLua.h"
#include "Entry/MainLua.h"
#include "Entity/EntityLua.h"
#include "Scene/SceneLua.h"
#include "Scene/SceneManagerLua.h"
#include "Combat/CombatLua.h"
#include "Player/ProgressLua.h"


ServiceImpLgc::ServiceImpLgc()
:m_wpSvcDB(NULL),
m_wpSvcHost(NULL),
m_pEttMgrWorld(NULL),
m_pEttMgrLogin(NULL),
m_pScnMgr(NULL),
m_pMsgHandler(NULL),
m_pScnDataMgr(NULL)
{
	
}

ServiceImpLgc::~ServiceImpLgc()
{
	if(m_pMsgHandler)
	{
		delete m_pMsgHandler;
		m_pMsgHandler = NULL;
	}
}

void
ServiceImpLgc::init(ServiceData& data)
{
	ServiceDataSMS* pSvcData = dynamic_cast<ServiceDataSMS*>(&data);
	if (pSvcData)
	{
		m_wpSvcDB = pSvcData->m_wpSvcDB;
		m_wpSvcHost = pSvcData->m_wpSvcHost;
	}
}

void
ServiceImpLgc::onStart()
{
	m_pEttMgrWorld = new EntityManager;

	m_pEttMgrLogin = new EntityManager(false);

	m_pScnMgr = new SceneManagerSvr;

	m_pScnDataMgr = new DataManagerImp;

	ProgressLua::libaray(Lua::instance()->state());
	DataLua::libaray(Lua::instance()->state());
	DataManagerLua::libaray(Lua::instance()->state());
	MainLua::libaray(Lua::instance()->state());
	EntityLua::libaray(Lua::instance()->state());
	CombatLua::libaray(Lua::instance()->state());
	SceneLua::libaray(Lua::instance()->state());
	SceneManagerLua::libaray(Lua::instance()->state());

	//
	//	initalize main lua script
	Lua::instance()->call("dofile('main.lua')");

	//
	//	initialize scene data manager from lua
	//DataManagerLua::push(Lua::instance()->state(), m_pScnDataMgr);
	//Lua::instance()->call("onLoadDataManagerPoint", 1, 0);

	initMsgHandler();

	__super::onStart();
}

void
ServiceImpLgc::onStop()
{
	__super::onStop();
}

void
ServiceImpLgc::onTimer()
{
	//LOG_DEBUG(" Timeout, Logic Thread!!!");

	__super::onTimer();
}

Service* ServiceImpLgc::getSvcDB() const
{
	return m_wpSvcDB;
}

Service* ServiceImpLgc::getSvcHost() const
{
	return m_wpSvcHost;
}


void
ServiceImpLgc::onDispatchMsg(IMsg* pMsg)
{
	//LOG_DEBUG(" Dispatch Message, Logic Thread!!!");
	
	if(m_pMsgHandler)
	{
		m_pMsgHandler->handleMsg(pMsg);
	}
}

/////////////////////////////////////////////////////


void 
ServiceImpLgc::setMsgHandler(int msgCate, IMsgHandler* pHandler)
{
	if(!m_pMsgHandler)
	{
		m_pMsgHandler = new MsgHandlerDispatchByCate;
	}

	m_pMsgHandler->addSubHandler(msgCate, 0,  pHandler);
}


void 
ServiceImpLgc::initMsgHandler()
{
	MsgHandlerInitDataLgc initData(m_wpSvcHost, this, m_wpSvcDB);
	initData.wpEttMgrL = m_pEttMgrLogin;
	initData.wpEttMgrW = m_pEttMgrWorld;
	initData.wpScnMgr = m_pScnMgr;
	initData.wpPlayerDataMgr = &m_playerDataMgr;

	LoginMsgHandler* pLMH = new LoginMsgHandler;
	if(pLMH)
	{
		pLMH->initialize(&initData);
		setMsgHandler(Msg::MC_Login, pLMH);
		setMsgHandler(Msg::MC_DB_Player, pLMH);
	}

	PlayerMsgHandler* pPMH = new PlayerMsgHandler;
	if(pPMH)
	{
		pPMH->initialize(&initData);
		setMsgHandler(Msg::MC_Player, pPMH);
		setMsgHandler(Msg::MC_DB_Role, pPMH);
		setMsgHandler(Msg::MC_DB_Item, pPMH);
		setMsgHandler(Msg::MC_DB_Form, pPMH);
		setMsgHandler(Msg::MC_DB_CoolDown, pPMH);
		setMsgHandler(Msg::MC_DB_Progress, pPMH);
	}

	SceneMsgHandler* pSMH = new SceneMsgHandler;
	if (pSMH)
	{
		pSMH->initialize(&initData);
	
		setMsgHandler(Msg::MC_Scene, pSMH);
	}
}



